package com.util;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.support.v4.app.NotificationCompat;

import com.firewarning.R;
import com.service.UserService;
import com.view.MainActivity;

import java.util.List;

/**
 * Created by Administrator on 2018/1/30.
 */

public class Message {
    /**
     * 直接调用短信接口发短信
     * @param phoneNumber
     * @param message
     */
    public static  void sendSMS(String phoneNumber,String message){
        //获取短信管理器
        android.telephony.SmsManager smsManager = android.telephony.SmsManager.getDefault();
        //拆分短信内容（手机短信长度限制）
        List<String> divideContents = smsManager.divideMessage(message);
        for (String text : divideContents) {
            smsManager.sendTextMessage(phoneNumber, null, text, null, null);
        }
    }
    public static  void playMusic(final Context context){
        new Thread(new Runnable() {
            @Override
            public void run() {
                MediaPlayer player=new MediaPlayer();
                try {
                    AssetFileDescriptor fileDescriptor = context.getAssets().openFd("test.mp3");
                    player.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(), fileDescriptor.getLength());
                    player.prepare();
                    player.start();
                }
                catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();

    }
    public static void  normalNotifaction (Context context){
        NotificationManager manager  = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setContentTitle("火灾通知");
        builder.setContentText(UserService.getSession(context).getAddress()+"发生火灾");
        builder.setAutoCancel(true);//可以点击通知栏的删除按钮删除
        builder.setSmallIcon(R.drawable.app_logo);//显示的小图标
        builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.app_logo));//下拉现实的额大图表
        PendingIntent pendingIntent = PendingIntent.getActivity(context,1, new Intent(context, MainActivity.class), 0);//点击实现跳转
        builder.setDefaults(NotificationCompat.DEFAULT_ALL);
        builder.setContentIntent(pendingIntent);
        Notification notification = builder.build();
        manager.notify(1, notification);
    }
}
